package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired
    private IDistrictService districtService;

    /**
     * 增加新地址
     *
     * @param uid
     * @param username
     * @param address
     */
    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        // 调用收货地址统计的方法
        Integer count = addressMapper.countByUid(uid);
        if (count >= 10) {
            throw new AddressCountLimitException("用户收货地址超过上限");
        }
        //对address对象中的数据进行补全:省市区等
        String provinceCode = districtService.getNameByCode(address.getProvinceCode());
        String cityCode = districtService.getNameByCode(address.getCityCode());
        String areaCode = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceCode(provinceCode);
        address.setCityCode(cityCode);
        address.setAreaCode(areaCode);

        address.setUid(uid);
        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(isDefault);

        address.setCreatedUser(username);
        address.setModifiedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date());

        //插入收货地方法
        Integer insert = addressMapper.insert(address);
        if (insert != 1) {
            throw new InsertEception("插入用户收货地址时候产生未知异常");
        }
    }

    /**
     * 根据uid获取address
     *
     * @param uid
     * @return
     */
    @Override
    public List<Address> getByUid(Integer uid) {
        return addressMapper.findByUid(uid);
    }

    /**
     * 设置默认地址
     *
     * @param aid
     * @param uid
     * @param username
     */
    @Override
    @Transactional//事务
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);

        if (result == null) {
            throw new AddressCountLimitException("尝试访问地址数据不存在");
        }
        //检测当前收到的收货地址的归属
        if (!result.getUid().equals(uid)) {
            throw new AccessDeniedException("非法数据访问");
        }
        //将所有地址设为非默认
        Integer integer = addressMapper.updateNonDefault(uid);
        if (integer < 1) {
            throw new UpdateException("更新数据出现未知的异常");
        }
        //将用户的某条地址设为默认
        Integer integer1 = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (integer1 != 1) {
            throw new UpdateException("更新数据出现未知的异常");
        }
    }

    /**
     * 删除用户收货地址
     *
     * @param aid      地址id
     * @param uid      用户id
     * @param username 用户名
     */
    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address byAid = addressMapper.findByAid(aid);
        if (byAid == null) {
            throw new AddressNotFoundException("收货地址数据不存在");
        }
        if (!byAid.getUid().equals(uid)) {
            throw new AccessDeniedException("非法数据访问");
        }
        Integer integer = addressMapper.deleteByAid(aid);
        if (integer != 1) {
            throw new DeleteException("删除数据出现未知异常");
        }
        Integer count = addressMapper.countByUid(uid);
        if (count == 0) {
            // 直接终止程序
            return;
        }
        Integer rows;

        if (byAid.getIsDefault() == 0) {
            return;
        }
        // 将这条数据中的is_default字符的值设置为1
        Address address = addressMapper.findLastModified(uid);
        rows = addressMapper.updateDefaultByAid(address.getAid(), username, new Date());

        if (rows != 1) {
            throw new UpdateException("更新时数据产生未知异常");
        }

    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        Address byAid = addressMapper.findByAid(aid);
        if (byAid == null) {
            throw new AddressNotFoundException("收货地址数据不存在");
        }
        if (!byAid.getUid().equals(uid)) {
            throw new AccessDeniedException("非法数据");
        }

        return byAid;
    }
}
